import axios from 'axios'
//获取后台后台接口的IP和端口号
import serverEnv from '../../../config/serverEnv'
axios.defaults.baseURL = serverEnv;
//公共方法引入
import utils from '../../libs/utils'
//解决axios post不能传递参数
import qs from 'qs'

const userList = {
	state: {
		userListAddSta: false, //新增用户模态框初始状态
		userListBranch: '', //部门
		userListNumber: '', //账号
		userListUserName: '', //姓名
		userListTel: '', //电话
		userListCurrent: 1, //当前页码
		userListPageSize: 10, //每页显示条数
		userListTotal: 1000000, //总条数
		userList: [] //数据列表
	},
	getters: {},
	mutations: {
		//修改新增用户模态框的隐藏显示状态
		userListAddStaMut: (state, payload) => {
			state.userListAddSta = !state.userListAddSta;
		},
		//根据条件查询列表
		userListMut: (state, payload) => {
			//把数据添加给state
			state.userListCurrent = payload.data.data.currentPage;
			state.userListPageSize = payload.data.data.pageSize;
			state.userListTotal = payload.data.data.totalSize;
			//获取后台的列表
			let dataList = payload.data.data.dataList;
			//组装
			for (let i = 0; i < dataList.length; i++) {
				let list = {};
				list.userid = dataList[i].userid;
				list.username = dataList[i].username;
				list.creater = dataList[i].creater;
				list.creatertime = dataList[i].creatertime;
				//把皓秒转换成日期
				list.creatertime = utils.millisecondToDate(dataList[i].creatertime)
				list.updater = dataList[i].updater;
				//把皓秒转换成日期
				list.updatertime = utils.millisecondToDate(dataList[i].updatertime)
				//用户身份判定
				if (dataList[i].identity == 1) {
					list.identity = 'root';
				} else if (dataList[i].identity == 2) {
					list.identity = 'admin';
				} else if (dataList[i].identity == 3) {
					list.identity = 'user';
				} else {
					list.identity = 'null';
				}
				//注入
				state.userList.push(list);
			}
		},
		//利用set对姓名进行数据修改
		userListUserNameMut: (state, payload) => {
			// console.log(payload);
			state.userListUserName = payload;
		}
	},
	actions: {
		//请求列表数据
		userListAct: (context) => {
			//清空userList数组
			context.state.userList = [];
			let obj = qs.stringify({
				username: context.state.userListUserName,
				page: context.state.userListCurrent,
				pageSize: context.state.userListPageSize,
			});
			//请求数据
			axios.post('/user/queryList', obj).then((res) => {
				context.commit('userListMut', res);
			})
		},
		//分页切换
		userListCurrentAct: (context, payload) => {
			//改变userListCurrent当前选中的页码
			context.state.userListCurrent = payload.newCurrent;
			//清空userList数组
			context.state.userList = [];
			//请求新页面的数据
			let obj = qs.stringify({
				username: context.state.userListUserName,
				page: context.state.userListCurrent,
				pageSize: context.state.userListPageSize,
			});
			axios.post('/user/queryList', obj).then((res) => {
				context.commit('userListMut', res);
			})
		},
		//利用set对姓名进行数据修改
		userListUserNameAct: (context, payload) => {
			context.commit('userListUserNameMut', payload.name);
		}
	}
};
export default userList;